package com.zhiwang.planet.mvvm.viewmodel.zygote

import androidx.lifecycle.MutableLiveData
import androidx.paging.PageKeyedDataSource
import androidx.paging.PagedList
import com.zhiwang.planet.mvvm.model.RestApi

abstract class SimplePageKeyedDataSource<KEY, VALUE> constructor(val restApi: RestApi) : PageKeyedDataSource<KEY, VALUE>() {

    val refreshingState = MutableLiveData<PagedList.LoadState>()

    val loadingState = MutableLiveData<PagedList.LoadState>()

    val initLoadDone = MutableLiveData<Boolean>()

    val errState = MutableLiveData<Throwable>()


    override fun loadInitial(params: LoadInitialParams<KEY>, callback: LoadInitialCallback<KEY, VALUE>) {

        initLoad(params, callback)
    }


    override fun loadBefore(params: LoadParams<KEY>, callback: LoadCallback<KEY, VALUE>) {
//        throw NotImplementedError("not support prepending operation!!!")
    }

    override fun loadAfter(params: LoadParams<KEY>, callback: LoadCallback<KEY, VALUE>) {

        loadNextPage(key = params.key, callback = callback)
    }

    abstract fun initLoad(params: LoadInitialParams<KEY>, callback: LoadInitialCallback<KEY, VALUE>)


    abstract fun loadNextPage(key: KEY, callback: LoadCallback<KEY, VALUE>)


}